package io.zhiller.ollamaclient.screen

import android.widget.Toast
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddComment
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import io.zhiller.ollamaclient.R
import io.zhiller.ollamaclient.screen.drawer.ScaffoldDrawerContent
import kotlinx.coroutines.launch

@Composable
fun MainScreen(navController: NavController) {
  val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
  
  // ModalNavigationDrawer 内容
  ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
    ScaffoldDrawerContent(navController, drawerState)
  }) {
    Scaffold(modifier = Modifier.fillMaxSize(), bottomBar = {
      Box(
        modifier = Modifier
          .fillMaxWidth()
          .height(0.dp)
      ) { }
    }, topBar = {
      ScaffoldTopAppBar(drawerState, navController)
    }) { innerPadding ->
      Surface(
        modifier = Modifier
          .padding(innerPadding)
          .fillMaxSize()
      ) {
      
      }
    }
  }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldTopAppBar(drawerState: DrawerState, navController: NavController) {
  val scope = rememberCoroutineScope()
  TopAppBar(
    modifier = Modifier,
    navigationIcon = {
      IconButton(onClick = {
        when {
          drawerState.isClosed -> {
            scope.launch {
              drawerState.open()
            }
          }
          
          else -> {
            scope.launch {
              drawerState.close()
            }
          }
        }
      }) {
        AnimatedContent(targetState = drawerState, label = "") { state ->
          if (state.isClosed) {
            Icon(
              imageVector = Icons.Default.Menu, contentDescription = "打开抽屉"
            )
          } else if (state.isOpen) {
            Icon(
              imageVector = Icons.Default.Close, contentDescription = "打开抽屉"
            )
          }
        }
      }
    },
    title = {
      Text(text = stringResource(R.string.app_name))
    },
    actions = {
      IconButton(onClick = {
//        NavActions(navController).navToPage(SupportPageRoutes.Search.route)
        Toast.makeText(
          navController.context,
          "已创建新的对话",
          Toast.LENGTH_SHORT
        ).show()
      }) {
        Icon(
          imageVector = Icons.Default.AddComment, contentDescription = "new chat",
        )
      }
    },
  )
}